//
//  MySQLRow.m
//  LibMySQL
//
//  Created by Marcus Grenängen on 2009-07-24.
//  Copyright 2009 SneWs Soft. All rights reserved.
//
#import "MySQLRow.h"

@interface MySQLRow (Private)

- (BOOL)_valueIsNumericAndNotNull:(id)value;
- (NSImage*)_valueAsImage:(id)value;

@end

@implementation MySQLRow

- (id)init {
	return [self initWithData: nil values: nil map: nil];
} // END METHOD: init:

- (id)initWithData:(NSArray *)columns values:(NSArray *)v map:(NSDictionary *)map {
	if (self = [super init]) {
		columnInfo = [columns retain];
		values = [v retain];
		columnMap = [map retain];
	
	} // END if (self = [super init])
	
	return self;
	
} // END METHOD: initWithData: 

- (void)dealloc {
	[columnInfo release];
	[columnMap release];
	[values release];

	[super dealloc];
	
} // END METHOD: dealloc:

#pragma mark -
#pragma mark Fetch methods
/*
 * @description: Returns the value associated with the specified column name.
 *
 * @returns: A pointer to a valid NS data type such as NSString or NSDate depending
 *			 on the data type the column is specified to have in MySQL.
 */
- (id)getValueByName:(NSString*)columnName {
	NSNumber *actualIndex = [columnMap valueForKey: columnName];
	if(actualIndex == nil || [actualIndex intValue] < 0 ) {
		[[NSException exceptionWithName: @"Invalid operation"
								 reason: [NSString stringWithFormat: @"The column name %@ is not valid", columnName]
							   userInfo: nil] raise];
		
	} // END if( actualIndex == nil || [actualIndex intValue] < 0 )
	
	return [values objectAtIndex: [actualIndex intValue]];
	
} // END METHOD: getValueByName: 

/*
 * @description: Returns the value associated with the specified column index.
 *
 * @returns: A pointer to a valid NS data type such as NSString or NSDate depending
 *			 on the data type the column is specified to have in MySQL.
 */
- (id)getValueByIndex:(NSInteger)columnIndex {
	if (columnIndex < 0 || columnIndex >= [values count])
		[[NSException exceptionWithName: @"Invalid operation" reason: @"The given column index is out of range." userInfo: nil] raise];
	
	return [values objectAtIndex: columnIndex];
	
} // END METHOD: getValueByIndex:

- (NSInteger)columnIndex:(NSString *)columnName {
	NSNumber *actualIndex = [columnMap valueForKey: columnName];
	if(actualIndex == nil || [actualIndex intValue] < 0 ) {
		[[NSException exceptionWithName: @"Invalid operation"
								 reason: [NSString stringWithFormat: @"The column name %@ is not valid", columnName]
							   userInfo: nil] raise];
		
	} // END if( actualIndex == nil || [actualIndex intValue] < 0 )
	
	return [actualIndex integerValue];
}

- (NSString *)columnName:(NSInteger)columnIndex {
	if (columnIndex < 0 || columnIndex >= [values count])
		[[NSException exceptionWithName: @"Invalid operation" reason: @"The given column index is out of range." userInfo: nil] raise];
	
	return (NSString *)[[columnMap allKeys] objectAtIndex: columnIndex];

} // END METHOD: columnName: 


#pragma mark -
#pragma mark Convenience methods such as Images etc...
- (NSInteger)intValueByName:(NSString *)columnName {
	id val = [self getValueByName: columnName];
	if( [self _valueIsNumericAndNotNull: val] )
		return [val intValue];
	
	return 0;
	
} // END METHOD: intValue: 

- (long)longValueByName:(NSString *)columnName {
	id val = [self getValueByName: columnName];
	if( [self _valueIsNumericAndNotNull: val] )
		return [val longValue];
	
	return 0;
}

- (double)doubleValueByName:(NSString *)columnName {
	id val = [self getValueByName: columnName];
	if( [self _valueIsNumericAndNotNull: val] )
		return [val doubleValue];
	
	return 0.0;
}

- (float)floatValueByName:(NSString *)columnName {
	id val = [self getValueByName: columnName];
	if( [self _valueIsNumericAndNotNull: val] )
		return [val floatValue];
	
	return 0.0f;
}

- (NSString *)stringValueByName:(NSString *)columnName {
	id val = [self getValueByName: columnName];
	if( val != nil ) {
		if( [val isKindOfClass: [NSNull class]] )
			return nil;
		
		return (NSString *)val;
	}
	
	return nil;
}

- (NSDate *)dateValueByName:(NSString *)columnName {
	id val = [self getValueByName: columnName];
	if( val != nil ) {
		if( [val isKindOfClass: [NSNull class]] )
			return nil;
		
		return (NSDate *)val;
	}
	
	return nil;
}

- (NSData *)dataValueByName:(NSString *)columnName {
	id val = [self getValueByName: columnName];
	if( val != nil ) {
		if( [val isKindOfClass: [NSNull class]] )
			return nil;
		
		return (NSData *)val;
	}
	
	return nil;
}


- (NSImage*)imageValueByName:(NSString*)columnName {
	return [self _valueAsImage: [self getValueByName: columnName]];
}

- (NSImage*)imageValueByIndex:(NSInteger)columnIndex {
	return [self _valueAsImage: [self getValueByIndex: columnIndex]];
}




- (BOOL)_valueIsNumericAndNotNull:(id)value {
	if( value != nil && ![value isKindOfClass: [NSNull class]] ) {
		if( [value isKindOfClass: [NSNumber class]] )
			return TRUE;
	}
	
	return FALSE;
}

- (NSImage*)_valueAsImage:(id)value {
	NSImage *oImage = nil;
	if (value != nil )// && [value class] == [NSData class] )
		oImage = [[NSImage alloc] initWithData: (NSData*)value];
	
	return [oImage autorelease];
}

@end